//
//  HYDataCheckVC.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYDataCheckVC.h"
#import "HYDataCheckView.h"
#import "HYDataCheckTableViewCell.h"
#import "HYDeviceDataApi.h"
#import "HYDateButtonView.h"
#import "DTPickerManager.h"
#import "MJRefresh.h"

@interface HYDataCheckVC ()<UITabBarDelegate,UITableViewDataSource>

@property(nonatomic,strong)HYDataCheckView *containerView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,assign)int page;

@property(nonatomic,strong)UILabel *emptyLabel;

@end

@implementation HYDataCheckVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.page = 1;
    [self updateDataListWithPage:self.page];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark --- 点击事件
- (void)didClickStartDateView
{
    __weak typeof(self) weakSelf = self;
    [[DTPickerManager shareManager] showDate:[NSDate date] pickerWithBirthDayAndPicker:^(NSArray *arr) {
        
    } andConfirm:^(NSArray *arr) {
        if (arr.count == 3)
        {
            NSString *time = [NSString stringWithFormat:@"%@-%@-%@",arr[0],arr[1],arr[2]];
            weakSelf.containerView.startView.dateLabel.text = time;
            [weakSelf updateDataListWithPage:1];
        }
    }];
}

- (void)didClickEndDateView
{
    __weak typeof(self) weakSelf = self;
    [[DTPickerManager shareManager] showDate:[NSDate date] pickerWithBirthDayAndPicker:^(NSArray *arr) {
        
    } andConfirm:^(NSArray *arr) {
        if (arr.count == 3)
        {
            NSString *time = [NSString stringWithFormat:@"%@-%@-%@",arr[0],arr[1],arr[2]];
            weakSelf.containerView.endView.dateLabel.text = time;
            [weakSelf updateDataListWithPage:1];
        }
    }];
}

#pragma mark --- 事件处理
- (void)updateDataListWithPage:(int)page
{
    NSString *pageStr = [NSString stringWithFormat:@"%i",page];
    NSString *sdate = _containerView.startView.dateLabel.text;
    NSString *edate = _containerView.endView.dateLabel.text;

    HYDeviceDataApi *api = [[HYDeviceDataApi alloc] initWithDic:@{@"page":pageStr,@"id":_deivceID,@"sdate":sdate,@"edate":edate}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [self.containerView.tableView.mj_footer endRefreshing];
        [self.containerView.tableView.mj_header endRefreshing];
        
        NSDictionary *data = request.responseJSONObject[@"data"];
        NSDictionary *list = data[@"list"];
        NSInteger total = [data[@"total"] intValue];
        
        if (self.page == 1)
        {
            [self.dataSource removeAllObjects];
            if (total > 0)
            {
                self.titleArray = list[@"title"];
                [self.dataSource addObjectsFromArray:list[@"data"]];
                [self.containerView.tableView reloadData];
                self.emptyLabel.hidden = YES;
            }
            else
            {
                [self.containerView.tableView reloadData];
                self.emptyLabel.hidden = NO;
            }
        }
        else
        {
            if ([list isKindOfClass:[NSDictionary class]])
            {
                [self.dataSource addObjectsFromArray:list[@"data"]];
                [self.containerView.tableView reloadData];
            }
            else
            {
                [self.containerView.tableView.mj_footer endRefreshingWithNoMoreData];

            }
            
        }


    } failure:^(YTKBaseRequest * _Nonnull request) {
        [self.containerView.tableView.mj_footer endRefreshing];
        [self.containerView.tableView.mj_header endRefreshing];
    }];
}

- (NSString *)getNowDateString
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:date];
}

- (NSString *)getBeforeSevenDayDateString
{
    NSDate *date = [NSDate date];
    NSInteger timeSp = [date timeIntervalSince1970];
    timeSp -= 6*24*3600;
    NSDate *orderDate = [NSDate dateWithTimeIntervalSince1970:timeSp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:orderDate];
}

#pragma mark tableView 代理
#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYDataCheckTableViewCell *cell = [HYDataCheckTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
    [cell updateDataWithTitleArray:_titleArray data:_dataSource[indexPath.section]];
//    cell.dateLabel.text = @"2017-10-01 00:20";
//    cell.tempLabel.text = @"温度：24";
//    cell.smogLabel.text = @"烟雾：1323";
//    cell.voltageLabel.text = @"电压：13232";
//    cell.electricityLabel.text = @"电流：18A";
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - --------------------------UITableView delegate--------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(374);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark --- 懒加载
- (HYDataCheckView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[HYDataCheckView alloc]init];
        [superView addSubview:_containerView];
        _containerView.tableView.delegate = self;
        _containerView.tableView.dataSource = self;
        _containerView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self updateDataListWithPage:self.page];
        }];
        _containerView.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            [self updateDataListWithPage:self.page];
        }];
        _containerView.startView.dateLabel.text = [self getBeforeSevenDayDateString];
        _containerView.endView.dateLabel.text = [self getNowDateString];
        UITapGestureRecognizer *tan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickStartDateView)];
        [_containerView.startView addGestureRecognizer:tan];
        UITapGestureRecognizer *tan2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickEndDateView)];
        [_containerView.endView addGestureRecognizer:tan2];
    }
    return _containerView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (UILabel *)emptyLabel
{
    if(!_emptyLabel)
    {
        UIView *superView = self.view;
        _emptyLabel = [[UILabel alloc]init];
        [superView addSubview:_emptyLabel];
        _emptyLabel.font = fontWithSize(15);
        _emptyLabel.textColor = [UIColor darkGrayColor];
        _emptyLabel.text = @"暂无满足条件的数据";
    }
    return _emptyLabel;
}

#pragma mark --- 布局
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.mas_equalTo(superView);
    }];
    
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(superView);
    }];
}

#pragma mark --- 导航栏
- (void)base_configNavigationBar
{
    [self setNavigationTitle:@"数据查询" titleColor:[UIColor blackColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"left"]];
    self.navigationBar.backgroundColor = [UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
